#include <cstdio>
#include <cstdlib>
#include <vector>
#include "glm/glm.hpp"
#include "glm/gtc/noise.hpp"
#include "glm/gtc/type_precision.hpp"

int main(int argc, char *argv[]) {
  //float t = glm::simplex(glm::vec3(1.2f, 2.4f, 3.5f));
  //printf("n = %f\n", t);
  //return 0;
  
  const int size = 800;
  std::vector<glm::u8vec4> image(size * size);

  for (int y = 0; y < size; ++y) {
    for (int x = 0; x < size; ++x) {
      const int idx = size * y + x;
      glm::vec3 c = 64.0f * glm::vec3(x / static_cast<float>(size),
                                     y / static_cast<float>(size),
                                     0.5f);
      const float d = 0.5f + 0.5f*glm::simplex(c);
      image[idx].b = static_cast<glm::u8>(255.0f * d);
      image[idx].g = static_cast<glm::u8>(255.0f * d);
      image[idx].r = static_cast<glm::u8>(255.0f * d);
      image[idx].a = 255;
    }
  }

  FILE *fp = std::fopen("snoise_ref.tga", "wb");
  const glm::u8 tga_header[18] = {
    0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    size & 0x00ff, (size & 0xff00) >> 8,
    size & 0x00ff, (size & 0xff00) >> 8,
    32, 0
  };
  std::fwrite(tga_header, sizeof(tga_header), 1, fp);
  std::fwrite(&image[0], size * size * 4, 1, fp);
  std::fclose(fp);
  return EXIT_SUCCESS;
}
